<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="网站标题" prop="tag">
        <el-input
          v-model="queryParams.tag"
          placeholder="请输入网站标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['iconT:icont:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['iconT:icont:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['iconT:icont:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['iconT:icont:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="icontList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
<!--      <el-table-column label="主键" align="center" prop="id"/>-->
      <el-table-column label="图标" align="center">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.iconUrl"
            fit="contain"
            :preview-src-list="[scope.row.iconUrl]"
            style="width: 30px; height: 30px"
          >
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="网站标题" align="center" prop="tag"/>
      <el-table-column label="使用状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 1 ? 'primary' : 'danger'">
            {{ scope.row.status === 1 ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['iconT:icont:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['iconT:icont:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改A-IconT对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="图标" prop="iconUrl">
          <el-upload
            class="icon-uploader"
            action="#"
            :http-request="handleUpload"
            :show-file-list="false"
            :before-upload="beforeUpload">
            <img v-if="form.iconUrl" :src="form.iconUrl" class="icon">
            <i v-else class="el-icon-plus icon-uploader-icon"></i>
          </el-upload>
          <!-- 添加和修改模式下都显示只读的图片地址 -->
          <el-input
            v-model="form.iconUrl"
            placeholder="请上传图片"
            style="margin-top: 10px;"
            readonly>
          </el-input>
        </el-form-item>
        <el-form-item label="网站标题" prop="tag">
          <el-input v-model="form.tag" placeholder="请输入网站标题"/>
        </el-form-item>
        <el-form-item label="使用状态" prop="status">
          <!-- 新增时只能选择禁用且默认为禁用，修改时根据条件限制启用选项 -->
          <el-radio-group
            v-model="form.status"
            :disabled="title === '添加A-IconT'">
            <el-radio
              label="1"
              :disabled="title === '修改A-IconT' && hasEnabled && form.status !== '1'">
              启用
            </el-radio>
            <el-radio label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


export default {
  name: "Icont",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // A-IconT表格数据
      icontList: [{

        "id": 5,
        "iconUrl": "@/assets/images/b.png",
        "tag": "医疗设备管理",
        "status": 1
      }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否已存在启用状态的记录
      hasEnabled: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        iconUrl: null,
        tag: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        iconUrl: [
          { required: true, message: "图标地址不能为空", trigger: "blur" }
        ],
        tag: [
          { required: true, message: "网站标题不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询A-IconT列表 */
    getList() {
      // this.loading = true;
      // listIcont(this.queryParams).then(response => {
      //   this.icontList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      //   // 检查是否存在启用状态的记录
      //   this.hasEnabled = this.icontList.some(item => item.status === '1');
      // });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        iconUrl: null,
        tag: null,
        status: "0" // 默认为禁用
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加A-IconT";
      this.form.status = "0";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.open = true;
      this.title = "修改A-IconT";
      // getIcont(id).then(response => {
      //   this.form = response.data;
      //   this.form.status = response.data.status.toString();
      //   this.open = true;
      //   this.title = "修改A-IconT";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            // 如果是修改操作，先更新状态，再更新当前记录的其他字段
            // updateStu(this.form).then(response => {
            //   // 然后更新当前记录的其他字段
            //   updateIcont(this.form).then(response => {
            //     this.$modal.msgSuccess("修改成功");
            //     this.open = false;
            //     this.getList();
            //   });
            // });
          } else {
            // 新增操作
            // addIcont(this.form).then(response => {
            //   this.$modal.msgSuccess("新增成功");
            //   this.open = false;
            //   this.getList();
            // });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除A-IconT编号为"' + ids + '"的数据项？').then(function() {
        // return delIcont(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('iconT/icont/export', {
        ...this.queryParams
      }, `icont_${new Date().getTime()}.xlsx`)
    },
    /** 上传图标文件 */
    handleUpload(param) {
      const formData = new FormData();
      formData.append("file", param.file);
      uploadIcon(formData).then(response => {
        this.form.iconUrl = response.url;
        this.$modal.msgSuccess("上传成功");
      }).catch(() => {
        this.$modal.msgError("上传失败");
      });
    },
    /** 上传前检查 */
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$modal.msgError('上传图标只能是 JPG/PNG 格式!');
      }
      if (!isLt2M) {
        this.$modal.msgError('上传图标大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }
};
</script>

<style scoped>
.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
}

.icon-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: border-color 0.2s ease;
}

.icon-uploader .el-upload:hover {
  border-color: #409EFF;
}

.icon-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}

.icon {
  width: 120px;
  height: 120px;
  display: block;
}
</style>
